package com.learn.controller;
 
import com.learn.entity.MyBean;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import java.util.Date;

/**
 * Spring 源码学习之在 Spring 源码中创建 web 工程
 * https://blog.csdn.net/weixin_39616159/article/details/113618296#comments_19002074
 */
@Controller
public class TestController {

	/**
	 * 测试 @Controller 的 DispatcherServlet 代码过程，
	 * 需要注释掉 springmvc.xml 中 SimpleUrlHandlerMapping 的配置。
	 * 访问URL：http://localhost:8080/learn_mvc/sayHello?age=17&name=lily
	 */
	@RequestMapping("sayHello")
	@ResponseBody
	public ModelAndView sayHello(int age, String name) {
		ModelAndView mav = new ModelAndView();
		MyBean bean = new MyBean(age, name);
		mav.addObject("myBean", bean);
		mav.setViewName("sayHello");
		System.out.println(bean.toString());
		return mav;
	}
	/* Output:
	 Hello world
	 姓名:lily
	 年龄:17
	 *///~

	// http://localhost:8080/learn_mvc/handle
	@RequestMapping("/handle")
	public String handle(Model model){
		Date date = new Date();
		model.addAttribute("date", date);
		return "success";
	}
	/* Output:
	跳转成功！服务器时间：Thu Nov 18 19:10:20 CST 2021
	*///~
}